package com.hyh.music.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.music.Result.PageResult;
import com.hyh.music.domain.domainDTO.SingerDTO;
import com.hyh.music.domain.domainDTO.SingerPageDTO;
import com.hyh.music.domain.domains.Singer;
import com.hyh.music.domain.domains.Song;
import com.hyh.music.mapper.SingerMapper;
import com.hyh.music.mapper.SongMapper;
import com.hyh.music.service.SingerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Slf4j
@Service
public class SingerServiceImpl implements SingerService {
    @Autowired
    SingerMapper singerMapper;
    @Autowired
    SongMapper songMapper;

    /**
     * 根据id查询歌手
     *
     * @param id
     * @return
     */
    @Override
    public Singer selectById(Integer id) {
        Singer singer = singerMapper.selectById(id);
        return singer;
    }

    /**
     * 歌手的分页查询
     *
     * @param singerPageDTO
     * @return
     */
    @Override
    public PageResult PageSelect(SingerPageDTO singerPageDTO) {
        PageHelper.startPage(singerPageDTO.getPage(), singerPageDTO.getPageSize());

        Page<Singer> singers = singerMapper.page(singerPageDTO);
        List<Singer> result = singers.getResult();
        long total = singers.getTotal();
        return new PageResult(total, result);
    }

    /**
     * 添加歌手
     *
     * @param singerDTO
     */
    @Override
    public void addSinger(SingerDTO singerDTO) {
        singerMapper.add(singerDTO);
    }

    /**
     * 删除歌手
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteSinger(Integer id) {
        List<Song> songs = songMapper.selectBySingerId(id);
        log.info("{}",songs);
        if (songs != null && songs.size() != 0) {
            return false;
        }
        singerMapper.deleteSingerById(id);
        return true;
    }

    /**
     * 更新歌手数据
     *
     * @param singer
     */
    @Override
    public void Update(Singer singer) {
        singerMapper.update(singer);
    }
}
